package com.ssm.hj.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ssm.hj.page.Page;
import com.ssm.hj.search.MemberSearch;
import com.ssm.hj.service.MemberService;
import com.ssm.hj.view.MemberStatisticsView;
import com.ssm.hj.view.MemberView;
import com.ssm.lzh.serachId.MemberId;
import com.ssm.mapper.MemberMapper;
import com.ssm.pojo.Member;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
public class MemberServiceImpl implements MemberService {
    @Resource
    private MemberMapper memberMapper;

    @Resource
    private MemberId memberId;

    @Override
    //阿杰 条件查询会员信息
    public PageInfo<MemberView> searchQueryMemberService(Page<MemberSearch> page) throws Exception {
        PageHelper.startPage(page.getPageNum(),page.getPageSize());
        return new PageInfo<MemberView>(this.memberMapper.searchQueryMember(page.getSearchObj()));
    }

    @Override
    //阿杰 修改状态
    public int updateState(String id, int state) throws Exception {
        return this.memberMapper.updateState(id,state);
    }

    @Override
    public int saveService(Member member) throws Exception {
          String id=  this.memberMapper.queryMemberIdLast();

        String memberId = this.memberId.createMemberId(id);
        this.memberMapper.insertMemberId(memberId);
        System.out.println(memberId);
        member.setFile_number(memberId);
        return this.memberMapper.save(member);
    }

    @Override
    public List<Member> queryMemberByName(String name) {
        return this.memberMapper.queryMemberByName(name);
    }

    @Override
    public  Map<String,MemberStatisticsView> queryMembertj(String sdate,String enddate) {
      List<MemberStatisticsView> l =
              this.memberMapper.queryMemberLoginRecord(sdate,enddate);

        List<MemberStatisticsView> l2 =
                this.memberMapper.queryBookingSum(sdate,enddate);
        Map<String,MemberStatisticsView> returnList=new HashMap<String,MemberStatisticsView>();

        System.out.println("登陆"+l);
        System.out.println("预约"+l2);
        if(l.size()>l2.size()){
            for (MemberStatisticsView m:l
             ) {
                for (MemberStatisticsView mm:l2
                ) {
                        if(m.getLogindate().equals(mm.getTime())){
                            System.out.println("queryBookingSum"+mm.getTime());
                            System.out.println("queryBookingSum"+mm.getBookingSum());
                            m.setTime(mm.getTime());
                            m.setBookingSum(mm.getBookingSum());
                        }
                }
                System.out.println(m);
                returnList.put(m.getLogindate(),m);
        }
        }else{
            for (MemberStatisticsView m:l2
            ) {
                for (MemberStatisticsView mm:l
                ) {
                    if(m.getTime().equals(mm.getLogindate())){
                        System.out.println("queryMemberLoginRecord"+mm.getLogindate());
                        System.out.println("queryMemberLoginRecord"+mm.getLoginSum());
                        m.setLogindate(mm.getLogindate());
                        m.setLoginSum(mm.getLoginSum());
                    }
                }
                returnList.put(m.getTime(),m);
            }
        }
        for (MemberStatisticsView m:l
             ) {
            if(!returnList.containsKey(m.getLogindate())){
                System.out.println("时间不存在登陆的");
                returnList.put(m.getLogindate(),m);
            }
        }

        for (MemberStatisticsView mm: l2
             ) {
           if(!returnList.containsKey(mm.getTime())){
               System.out.println("时间不存在预约的的");
               returnList.put(mm.getTime(),mm);
           }
        }

        System.out.println(returnList);
        return returnList;
    }
}
